/*
 *      You may freely copy, distribute and reuse the code
 *      in this example.  Scott Anguish disclaims any warranty of
 *      any kind, expressed or implied, as to its fitness
 *      for any particular use.
 *		Please keep this notice intact
 *      Written by: Scott Anguish  sanguish@digifix.com
 */

#import <appkit/appkit.h>

#import <streams/streams.h>

#import	<stdio.h> 

#import "Controller.h"
#import "MMCell.h"
#import "MMFolderCell.h"
#import "MMDocumentCell.h"
#import "MMGraphicCell.h"
#import "MMText.h"

@implementation Controller

+ new
{
    self = [super new];
    OpenPanelObject = [OpenPanel new];
    [OpenPanelObject allowMultipleFiles:NO];
	[NXApp setDelegate:self];
    return(self);
}

- init
{
	[super init];
	return self;
};


- free
{
    [OpenPanelObject free];
    [super free];
    return(self);
}
      
-(int)appOpenFile:(char *)fileName type:(char *)fileType
{
    char tempfilename[1024];
	[self replaceText];
	[[myScrollView window] makeKeyAndOrderFront: nil];

    strcpy(tempfilename, fileName);
    [[myScrollView docView] openFileName:tempfilename ignoreHeader:YES];
    return YES;
}


- (BOOL)appAcceptsAnotherFile:sender
{
    return YES;
}


- print:sender
{
	[[myScrollView docView] printPSCode:self];
	return self;
};


- appDidInit:sender
{
	
	[[NXApp appListener] setServicesDelegate: self];
	[self replaceText];
	[[myScrollView window] makeKeyAndOrderFront: nil];

	return self;
}





- replaceText
{
	
	NXStream *s = (NXStream *)nil;
	id		stdDoc, newDoc;
	NXRect	r;
		
	/* Measure the old doc view, then trash it. */
	stdDoc = [myScrollView docView];
	[stdDoc getFrame:&r];

	/* Get the entire text, using the rich RTF format */
	s = NXOpenMemory(NULL, 0, NX_READWRITE);
	if (s)   [stdDoc writeRichText: s];

	[myScrollView setVertScrollerRequired:YES];
	[myScrollView setHorizScrollerRequired:NO];
	[myScrollView setDynamicScrolling:YES]; 
	
	newDoc = [[MMText alloc] initFrame:&r];
	[newDoc moveTo:0.0:0.0];
	[newDoc notifyAncestorWhenFrameChanged: YES];
	[newDoc setVertResizable:YES];
	[newDoc setSelectable:YES];
	[newDoc setEditable: NO];
	[newDoc setAutosizing:NX_WIDTHSIZABLE];
	[newDoc setMinSize:&r.size];
	[newDoc setMonoFont:NO];
	r.size.height = 1.0e30;
	[newDoc setMaxSize:&r.size];
	[myScrollView setDocView:newDoc];
	[stdDoc free];
	
	/* Stick the text from the original doc into the new doc */
	if (s)
	{
		/* Rewind to the beginning of the stream */
		NXSeek(s, 0L, NX_FROMSTART);
		[newDoc readRichText:s];
		NXCloseMemory(s, NX_FREEBUFFER);
	}

	return self;
}

- msgOpen:(id)pbid userData:(const char *)udata error:(char **)errmsg
{
	char *data; 
	int   length;
	const char *const *types;
	int   hasAscii, i;
	
		types = [pbid types];		/* get a list of pasteboard types */
		hasAscii = 0;
		for(i=0; !hasAscii && types[i]; i++)
			if(!strcmp(types[i], NXAsciiPboardType)) hasAscii = 1;
		if(hasAscii)
		{
			[pbid readType:NXAsciiPboardType data:&data length:&length];
			if(data && length)
			{
			
			[[myScrollView docView] readFromMemory:data length:length];
			
			} /* end if(data && length) */
			else
			*errmsg = "Selection is empty.";
			vm_deallocate(task_self(), data, length);
		}  /* end if(hasAscii) */
		else
			*errmsg = "No NG text found in your selection.";
		return self;
}

@end
